#ifndef COMMON_H_
#define COMMON_H_

#include "lame.h"

#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <netinet/in.h>

typedef struct lame_global_struct lame_global_flags;
typedef unsigned char byte;
typedef unsigned int uint32;
typedef int int32;
typedef unsigned short uint16;

extern int const WAV_ID_RIFF; /* "RIFF" */
extern int const WAV_ID_WAVE; /* "WAVE" */
extern int const WAV_ID_FMT; /* "fmt " */
extern int const WAV_ID_DATA; /* "data" */

const int MAX_BUF_LEN = 1000000; // 1MB
enum BYTE_ORDER_T { ENDIAN_LITTLE = 0, ENDIAN_BIG };

int readn(int32 fd, byte* buf, uint32 nbytes);

int writen(int32 fd, const byte* buf, uint32 nbytes);

class Item
{
    public:
        byte _data[MAX_BUF_LEN]; // TODO: [liangrongtang] Replace it with MAX_MP3_BUF_LEN
        uint32 _dataLen;

        Item();
        void Init();
};

/**
 * @brief: Parse IP-string from struct sockaddr_in
 * @param [in] addr: struct sockaddr_in
 * @return: IP-string
 **/
std::string GetIP(struct sockaddr_in* addr);

uint16 GetPort(struct sockaddr_in* addr);

enum LAN { ZH, EN };

enum ENCODING { GBK };

/**
 * TTS REQ CONST
 **/
const int jTTS_HEADER_LEN = 12;
const int jTTS_ID = 0x5A5A5A5A;
const int jTTS_REQ_TEXT4 = 0x1015;
const int jTTS_ANS_CONNECT = 0x4000;
const int jTTS_ANS_SOUND4 = 0x4003;
const int jTTS_REQ_QUIT = 0x1100;
const int jTTS_NOERROR = 0x0000;
const int jTTS_MOREDATA = 0xA0040001;
const int jTTS_REQ_SETPARAM4 = 0x1010;
const int jTTS_ANS_SETPARAM = 0x4001;

extern const uint32 WAV_HEADER_LEN;
const uint32 MAX_TEXT_BUF_LEN = 500;
typedef struct sockaddr sockaddr;
typedef struct sockaddr_in sockaddr_in;

#endif /* COMMON_H_ */
